<template>
  <div v-if="model" class="editViews">
    <div style="margin-bottom: 10px"><Navbar /></div>
    <div class="uploadfile">
      <van-uploader :after-read="afterRead" preview-size="100vw" class="uploadimg" />
      <editBanner left="头像">
        <img :src="model.user_img"  slot="right" v-if="model.user_img">
        <img src="../assets/logo.png"  slot="right" v-else>
      </editBanner>
    </div>
    <editBanner left="昵称" @bannerClick="show = true">
      <a href="#" slot="right">{{model.name}}</a>
    </editBanner>
    <editBanner left="账号">
      <a href="#" slot="right">{{model.username}}</a>
    </editBanner>
    <editBanner
    left="性别"
    @bannerClick="gendershow = true"
    >
      <a href="#" slot="right">{{model.gender == 1 ? '男' : '女'}}</a>
    </editBanner>
    <editBanner left="个性签名" @bannerClick="textshow = true">
    </editBanner>
    <!-- 修改昵称 -->
    <van-dialog
    v-model="show"
    title="昵称"
    show-cancel-button
    @confirm="confirmClick"
    @cancel="content=''"
    >
      <van-field v-model="content" autofocus />
    </van-dialog>
    <!-- 修改个性签名 -->
    <van-dialog
    v-model="textshow"
    title="个性签名"
    show-cancel-button
    @confirm="textareaClick"
    @cancel="content=''"
    >
      <van-field v-model="content" autofocus type="textarea" />
    </van-dialog>
    <!-- 性别 -->
    <van-action-sheet cancel-text="取消" v-model="gendershow" :actions="actions" @select="onSelect" />
  </div>
</template>

<script>
import Navbar from '@/components/common/Navbar'
import editBanner from '@/components/common/editBanner'
export default {
  data () {
    return {
      model: {},
      show: false,
      content: '',
      textshow: false,
      gendershow: false,
      actions: [{ name: '女', val: 0 }, { name: '男', val: 1 }]
    }
  },
  components: {
    Navbar,
    editBanner
  },
  methods: {
    async selectUser () {
      const res = await this.$http.get('/user/' + localStorage.getItem('id'))
      this.model = res.data[0]
    },
    // 头像上传(file为后端规定的)
    async afterRead (files) {
      const fromdata = new FormData()
      fromdata.append('file', files.file)
      const res = await this.$http.post('/upload', fromdata)
      this.model.user_img = res.data.url
      this.UserUpdata()
    },
    async UserUpdata () {
      const res = await this.$http.post('/update/' + localStorage.getItem('id'), this.model)
      if (res.data.code === 200) {
        this.$msg.fail('修改成功')
      }
      console.log(res)
    },
    confirmClick () {
      this.model.name = this.content
      this.UserUpdata()
      this.content = ''
    },
    textareaClick () {
      this.model.user_desc = this.content
      this.UserUpdata()
      this.content = ''
    },
    onSelect (data) {
      this.model.gender = data.val
      this.UserUpdata()
      this.gendershow = false
      this.$msg.fail('修改成功')
    }
  },
  created () {
    this.selectUser()
  }
}
</script>

<style lang="scss" scoped>
.editViews img {
  height: 46px;
  width: 46px;
  border-radius: 50%;
}
a {
  color: black;
}
.uploadfile {
  position: relative;
  overflow: hidden;
  .uploadimg {
    position: absolute;
    opacity: 0;
  }
}
</style>
